package com.xbongbong.paas.toolbox.pojo;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 权限项清单
 * @author kaka
 * @time 2018-10-16 10:16
 */
@Getter
@Setter
@ToString
public class PermissionTermsPojo implements Serializable {

    private static final long serialVersionUID = 3523585321701097186L;

    //==================== 表单权限 ====================

    /**
     * 查看权限
     */
    private Integer read;

    /**
     * 新建权限
     */
    private Integer create;

    /**
     * 更新权限
     */
    private Integer  update;

    /**
     * 删除权限
     */
    private Integer delete;

    /**
     * 导入权限，为与关键字import区分
     */
    private Integer imports;

    /**
     * 导出权限
     */
    private Integer export;

    /**
     * 打印权限
     */
    private Integer print;

    /**
     * e签宝签署
     */
    private Integer esign;

    /**
     * 文件柜可见
     */
    private Integer fileCabinetVisible;

    /**
     * 文件柜删除
     */
    private Integer fileCabinetDelete;

    /**
     * 移交
     */
    private Integer handleOver;


    //==================== 数据权限 ====================

    /**
     * 数据权限
     */
    private Integer dataPermission;

    public PermissionTermsPojo() {
        this.read = 0;
        this.create = 0;
        this.update = 0;
        this.delete = 0;
        this.imports = 0;
        this.export = 0;
        this.print = 0;
        this.esign = 0;
        this.fileCabinetVisible = 0;
        this.fileCabinetDelete = 0;
        this.handleOver = 0;
        this.dataPermission = 1;
    }

    public PermissionTermsPojo(boolean isAdminOrBoss) {
        this();
        if (isAdminOrBoss) {
            this.read = 1;
            this.create = 1;
            this.update = 1;
            this.delete = 1;
            this.imports = 1;
            this.export = 1;
            this.print = 1;
            this.esign = 1;
            this.fileCabinetVisible = 1;
            this.fileCabinetDelete = 1;
            this.handleOver = 1;
            this.dataPermission = 5;
        }
    }

    /**
     * 计算本PermissionTermsPojo 和 another PermissionTermsPojo 的最大权限
     * 并用最大权限覆盖本PermissionTermsPojo
     * 返回本身是为了能进行链式操作
     * @param another 另一个PermissionTermsPojo
     */
    public PermissionTermsPojo maxPermission(PermissionTermsPojo another) {
        this.read = this.read | another.read;
        this.create = this.create | another.create;
        this.update = this.update | another.update;
        this.delete = this.delete | another.delete;
        this.imports = this.imports | another.imports;
        this.export = this.export | another.export;
        this.print = this.print | another.print;
        this.esign = this.esign | another.esign;
        this.fileCabinetVisible = this.fileCabinetVisible | another.fileCabinetVisible;
        this.fileCabinetDelete = this.fileCabinetDelete | another.fileCabinetDelete;
        this.handleOver = this.handleOver | another.handleOver;
        this.dataPermission = another.dataPermission > this.dataPermission ? another.dataPermission : this.dataPermission;
        return this;
    }
}
